package org.anyline.data.jdbc.adapter.init;

import org.anyline.data.param.ConfigStore;
import org.anyline.data.run.Run;
import org.anyline.data.runtime.DataRuntime;
import org.anyline.entity.Compare;
import org.anyline.entity.OrderStore;
import org.anyline.entity.PageNavi;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public abstract class MySQLGenusAdapter extends SQLAdapter {

    @Override
    public String insertHead(ConfigStore configs){
        Boolean override = null;
        if(null != configs){
            override = configs.override();
        }
        if(null == override){
            return "INSERT INTO ";
        }else{
            if(override){
                return "REPLACE INTO ";
            }else{
                return "INSERT IGNORE INTO ";
            }
        }
    }
    @Override
    public String mergeFinalQuery(DataRuntime runtime, Run run){
        String sql = run.getBaseQuery();
        String cols = run.getQueryColumn();
        if(!"*".equals(cols)){
            String reg = "(?i)^select[\\s\\S]+from";
            sql = sql.replaceAll(reg,"SELECT "+cols+" FROM ");
        }
        OrderStore orders = run.getOrderStore();
        if(null != orders){
            sql += orders.getRunText(getDelimiterFr()+getDelimiterTo());
        }
        PageNavi navi = run.getPageNavi();
        if(null != navi){
            long limit = navi.getLastRow() - navi.getFirstRow() + 1;
            if(limit < 0){
                limit = 0;
            }
            //sql += " LIMIT " + navi.getFirstRow() + "," + limit;
            sql += " LIMIT " +  limit + " OFFSET " + navi.getFirstRow();
        }
        sql = sql.replaceAll("WHERE\\s*1=1\\s*AND", "WHERE");
        return sql;
    }

    /**
     * 构造 FIND_IN_SET 查询条件
     * 如果不需要占位符 返回null  否则原样返回value
     * @param builder builder
     * @param compare 比较方式 默认 equal 多个值默认 in
     * @param column 列
     * @param value value
     * @return value
     */
    @Override
    public Object createConditionFindInSet(DataRuntime runtime, StringBuilder builder, String column, Compare compare, Object value){
        List<Object> values = new ArrayList<>();
        if(null != value){
            if(value instanceof Collection){
                Collection cols = (Collection) value;
                for(Object col:cols){
                    values.add(col);
                }
            }else if(value instanceof Object[]){
                Object[] array = (Object[]) value;
                for(Object obj:array){
                    values.add(obj);
                }
            }else{
                values.add(value);
            }
        }
        if(values.size() > 1){
            builder.append("(");
        }
        boolean first = true;
        for(Object v:values){
            if(!first){
                builder.append(" OR ");
            }
            builder.append("FIND_IN_SET(?,").append(column).append(")");
            first = false;
        }
        if(values.size() > 1){
            builder.append(")");
        }
        return value;
    }
    public String concat(DataRuntime runtime, String ... args){
        return concatFun(args);
    }

}
